package com.cws.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;







import org.apache.commons.io.FilenameUtils;

import com.cws.util.WebUtil;
import com.cws.web.UserSession;

public class GlobalSessionFilter implements Filter {
	
    public static final String PARAM_NAME_EXCLUSIONS  = "exclusions";
    
    private Set<String> excludesPath = Collections.emptySet();
    
    private String contextPath;
    
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		String  exclusions = filterConfig.getInitParameter(PARAM_NAME_EXCLUSIONS);
		excludesPath = new HashSet<String>(Arrays.asList(exclusions.split(",")));
		contextPath = WebUtil.getContextPath(filterConfig.getServletContext());
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		String requestURI= httpRequest.getRequestURI();
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		UserSession userSession = (UserSession) httpRequest.getSession().getAttribute(UserSession.USER_SESSION_KEY);
//		if(isExclusion(requestURI)){
//			if(null == userSession){
//				httpResponse.sendRedirect("/demo/ajax/loginPage.do");
//			}
//		}else{
			System.out.println("==============chain.doFilter-- before================");
			chain.doFilter(httpRequest, httpResponse);
			System.out.println("==============chain.doFilter-- after================");   
//		}

	}
	
    public boolean isExclusion(String requestURI) {
        if (excludesPath == null) {
            return false;
        }

        if (contextPath != null && requestURI.startsWith(contextPath)) {
            requestURI = requestURI.substring(contextPath.length());
            if (!requestURI.startsWith("/")) {
                requestURI = "/" + requestURI;
            }
        }
        boolean flag = false;
        for (String pattern : excludesPath) {
        	if(FilenameUtils.wildcardMatch(requestURI, pattern)){
        		flag = true;
        	}
        }

        return flag;
    }
	
	
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}
	public static void main(String[] args) {
//		System.out.println("*/ajax/loginPage.do".matches("./ajax/.*.do"));
//		System.out.println("*/ajax/loginPage2.do".matches("./ajax/.*.do"));
//		System.out.println("demo/ajax/loginPage23.do".matches(".*/ajax/.*.do"));
		System.out.println("a.do".matches(".*.do"));
		System.out.println("ajax/a.do".matches("ajax/.*.do"));
		
	}
}
